package com.yst.suivi.services.reports;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

import com.yst.suivi.comparator.NameObjectComparator;
import com.yst.suivi.data.reports.Report;
import com.yst.suivi.data.reports.ReportLocalHome;
import com.yst.suivi.data.reports.ReportUtil;
import com.yst.suivi.services.AbstractSessionBean;

/**
 * @ejb.bean display-name="ReportService" jndi-name="ReportHomeRemote"
 *           name="ReportView" type="Stateless" view-type="both"
 */
public class ReportServiceBean extends AbstractSessionBean implements SessionBean
{
	/**
	 * @throws CreateException
	 * @ejb.create-method view-type = "both"
	 */
	public void ejbCreate() throws CreateException
	{
		// by default nothing to do
	}

	/**
	 * This method create a new report.
	 * 
	 * @param standard
	 * @return standard updated
	 * @throws CreateException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public String createReport(String nom, String filename, String reportPath, int type) throws NamingException, CreateException
	{
		checkConstraints(nom, "".intern());
		checkConstraints(filename, "".intern());
		checkConstraints(reportPath, "".intern());
		ReportLocalHome home = ReportUtil.getLocalHome();
		Report report = home.create();
		updateObject(nom, filename, reportPath, report, type);
		return report.getId();
	}

	/**
	 * The method checks if all the business constraints are verified.
	 * 
	 * @param login
	 * @param nom
	 * @param prenom
	 */
	private void checkConstraints(String nom, String id)
	{
		String[] parameter =
		{ "report", "nom" };
		checkEmptyField(nom, parameter);
		checkFieldLength(nom, SIMPlE_FIELD_MAX_LENGTH, "nom");
	}

	/**
	 * This method update a Report.
	 * 
	 * @return standard updated
	 * @throws NamingException
	 * @throws FinderException
	 * ejb.interface-method view-type = "both"
	 * ejb.transaction type = "Required"
	 */
	protected void storeReport(String id, String nom, String filename, String path, int type) throws FinderException, NamingException
	{

		ReportLocalHome home = ReportUtil.getLocalHome();
		Report report = home.findByPrimaryKey(id);
		checkConstraints(nom, id);
		updateObject(nom, filename, path, report, type);
	}

	/**
	 * La methode remplie l'objet EJB avec les valeurs fournies
	 * 
	 * @param annee
	 * @param mois
	 * @param jour
	 * @param tpsPos
	 * @param prj
	 */
	private void updateObject(String nom, String filename, String path, Report report, int type)
	{
		report.setNom(nom);
		report.setFilename(filename);
		report.setReportPath(path);
		report.setType(type);
	}

	/**
	 * This method delete a Report.
	 * 
	 * @param standard
	 * @throws NamingException
	 * @throws RemoveException
	 * @throws EJBException
	 * @throws FinderException 
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void deleteReport(String id) throws NamingException, EJBException, RemoveException, FinderException
	{
		ReportLocalHome home = ReportUtil.getLocalHome();
		home.remove(id);
	}

	/**
	 * The method returns all the report
	 * 
	 * @return
	 * @throws FinderException
	 * @throws NamingException
	 * @ejb.interface-method view-type = "both"
	 */
	public Collection findReport() throws FinderException, NamingException
	{
		
		ArrayList retour = new ArrayList(ReportUtil.getLocalHome().findAll());
		Collections.sort(retour, NameObjectComparator.comparator);
		return retour;
	}
	
	/**
	 * The method returns all the report
	 * 
	 * @return
	 * @throws FinderException
	 * @throws NamingException
	 * @ejb.interface-method view-type = "both"
	 */
	public Collection findReport(int type) throws FinderException, NamingException
	{
		
		ArrayList retour = new ArrayList(ReportUtil.getLocalHome().findByType(type));
		Collections.sort(retour, NameObjectComparator.comparator);
		return retour;
	}

	/**
	 * The method returns the folder identify by the primary key
	 * 
	 * @param folderKey
	 * @return
	 * @throws FinderException
	 * @throws NamingException.
	 * @ejb.interface-method view-type = "both"
	 */
	public Report findReportByKey(String folderKey) throws FinderException, NamingException
	{
		return ReportUtil.getLocalHome().findByPrimaryKey(folderKey);
	}
}
